#!/usr/bin/python3
# coding=UTF-8
import sys
import os
import json

=======
kwsPath = 'keywords.txt'
inputJson= 'input.json'
#verbose=False
#decode_online=False

def getKeywords(text, words):

	n=len(text)
	hash={}
	for word in words:
		hash[word]=1
	f=[0 for i in range(0,n+1)]
	s=[0 for i in range(0,n+1)]
	s[0]=-1
	for i in range(1,n+1):
	#	print("---iter i=%d---"%i)
		st=i-1
		f[i]=f[i-1]
		s[i]=s[i-1]
		for word in words:
			if st-len(word)+1>=0:
				if text[i-len(word):i]==word and f[i-len(word)]+len(word)>f[i]:
					f[i]=f[i-len(word)]+len(word)
					s[i]=i-len(word)
	#				print("update: f[%d]: %d" %(i, f[i]))
	#print(f[n],s[n])
	p=n
	r=f[n]
	toks=[]
	while p!=-1:
		q=s[p]
		v=r-f[q]
#		print(q,text[q:q+v])
		if text[q:q+v] in hash:
			toks.append(text[q:q+v])
		p=q
		r=f[q]
	toks.reverse()
#	print(toks)
	ret="".join(toks)
	return ret




def calc_cer(checklist, decode_online, verbose):

	words=[]
	keywords={}
	tmp=0
	with open(kwsPath,"r",encoding="utf-8") as f:
		for line in f.readlines():
		   words.append(line.strip('\n').replace(' ',''))
		   keywords[line.strip('\n').replace(' ','')]=1
	TP,FP,FN,TN=0,0,0,0
	index=0
	for decodefile, textfile in checklist.items():
		std={}
		text={}
		#print(textfile)
		f=open(textfile, "r",encoding="utf-8")
		for line in f.readlines():
			toks=line.strip('\n').split(" ")
			uuid=toks[0]
			text[uuid]="".join(toks[1:])
			content=getKeywords(text[uuid], words)
#			print("text %s | %s"%( line.strip('\n'), content))
			std[uuid]=content
		f.close()
		print("---decode---")
		print("online:", decode_online)
		df=open(decodefile, "r", encoding="utf-8")
		#f=open(decodefile, "r")
		for line in df.readlines():
			id=""
			content=""
			if decode_online==False:
				toks=line.strip('\n').split(' ',1)
				id=toks[0]
				if id in std:
					content="".join(toks[1].replace('<filler>','').split(' '))
			else:
				index+=1
				p=line.find('LOG')
				id=(line.split(' ')[-1])
				q=id.rfind('/')
				if q==-1 :
					continue
				id=id[q+1:-5]
				if id in std:
					content="".join(line[:p].replace('<filler>','').split(' '))					
					
			if id in std:
				#print(id,': ',line.strip('\n'),'-->', content)
				#print(id,': ', content)
				result = '| %-8s | %-15s | %-15s | %-15s |'%( id , content, text[id], std[id])
				if content==std[id]:
					if std[id]!='':
						TP+=1
						result+=" TP |"
						if verbose:
							print(result)
				else:
					if std[id]=='' and content!='':
						if content in keywords:
							FP+=1
							result+=" FP |"
						else:
							TN+=1
							result+=" TN |"
						if verbose:
							print(result)
					if std[id]!='':
						if content =='' or content!=std[id]:
							FN+=1
							result+=" FN |"
						if verbose:
							print(result)

		print("----------------")
		df.close()


	print("TP: %-6d FP: %-6d TN: %-6d FN: %-6d"%(TP,FP,TN,FN))
	P=TP/(TP+FP)
	R=TP/(TP+FN)
	F=2*P*R/(P+R)
	print("P=%.3f%% \t R=%.3f%% \t F=%.3f%% "%(100*P, 100*R, 100*F))

if __name__=='__main__':

	with open(inputJson,"r",encoding="utf-8") as f:
		config=json.load(f)
	verbose, decode_online = config['verbose'], config['online']
	for taskName, checklist in config.items():
		if taskName=="verbose" or taskName=="online":
			#print(taskName, checklist)
			continue
	#	print("------------------ %s --------------------"%checklist)
		try:
			calc_cer(checklist, decode_online, verbose)
		except:
			print(sys.exc_info()[0])
		print()
